package at.guigu.config;

import at.guigu.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

// 该注解代表该类是SpringMVC的核心配置类
@Configuration
// 配置注解的组件扫描<context:component-scan base-package="at.guigu.controller"></context:component-scan>
// 加载controller对应的bean
@ComponentScan({"at.guigu.controller", "at.guigu.interceptor", "at.guigu.config"})
// 自动配置 Spring MVC 的各种特性
@EnableWebMvc
public class SpringMvcConfiguration implements WebMvcConfigurer {

    // 配置视图解析器
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/jsp/");  // 设置视图文件路径前缀
        resolver.setSuffix(".jsp");   // 设置视图文件后缀
        return resolver;
    }
}

/*
// 该注解代表该类是SpringMVC的核心配置类
@Configuration
// 配置注解的组件扫描<context:component-scan base-package="at.guigu.controller"></context:component-scan>
// 加载controller对应的bean
@ComponentScan("at.guigu.controller")
// 自动配置 Spring MVC 的各种特性
@EnableWebMvc
public class SpringMvcConfiguration implements WebMvcConfigurer {
    // 依赖注入拦截器的bean
    @Autowired
    private MyInterceptor myInterceptor;

    // 配置视图解析器
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/jsp/");  // 设置视图文件路径前缀
        resolver.setSuffix(".jsp");   // 设置视图文件后缀
        return resolver;
    }

    // 配置静态资源路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源路径映射
        registry.addResourceHandler("/xxx/**").addResourceLocations("/xxx/");
    }

    // 配置拦截器路径
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //addInterceptor定义拦截器的具体实现类
        //addPathPatterns的参数代表拦截所有请求路径
        registry.addInterceptor(myInterceptor).addPathPatterns("/**");
    }
}
* */